package com.banmao.youZone;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.banmao.youZone.mapper.UserMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Timer;
import java.util.TimerTask;

@Slf4j
@EnableScheduling
@SpringBootApplication
public class YouZoneApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(YouZoneApplication.class, args);
    }

    @Resource
    private UserMapper userMapper;

    @Override
    public void run(String... args) throws Exception {
        userMapper.selectById(1);
        log.info("youZone start...");

        String frogSecondTime = System.getProperty("frogSecondTime");
        if (frogSecondTime == null || frogSecondTime.isEmpty()) {
            frogSecondTime = "60";
        }
        log.info("[FrogLog]frogSecondTime:{}", frogSecondTime);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // frog log
                int logType = RandomUtil.randomInt(0, 4);
                DateTime current = DateUtil.date();
                try {
                    switch (logType) {
                        case 0:
                            log.info("[FrogLog]Call Log at:{}", current);
                            break;
                        case 1:
                            log.warn("[FrogLog]Call Log at:{}", current);
                            break;
                        case 2:
                            log.debug("[FrogLog]Call Log at:{}", current);
                            break;
                        default:
                            log.error("[FrogLog]Call Log at:{}", current);
                            throw new IllegalArgumentException("panic!!!");
                    }
                } catch (Exception e) {
                    log.error("[FrogLog]发生异常,msg:{},err:", e.getMessage(), e);
                }
            }
        }, 1000, 1000L * Integer.parseInt(frogSecondTime));

    }
}
